import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    // element-plus主题色
    ElementPlus({
      useSource: true,
    }),
    vue(),
    vueJsx(),
    vueDevTools(),
    // element-plus按需引入
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
        @use "@/styles/element/index.scss";
        @use "@/styles/varibles.scss" as *;
        `
      }
    },
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    // 代理配置，访问/api模拟访问/public/mock下的文件
    proxy: {
      '/api': {
        target: 'http://localhost:5173', // 开发服务器地址（默认 Vite 端口）
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/mock')
      }
    }
  }
})
